
package Managers;

import MIDlet.FlunkerMIDlet;
import MIDlet.FlunkerCanvas;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m3g.Loader;
import javax.microedition.m3g.World;

public class FlunkerManager {
    
    // The MIDlet
    private FlunkerMIDlet flunker_midlet;
    // The Canvas
    private FlunkerCanvas flunker_canvas;
    // The Display
    private Display flunker_display;
    // The Communication Manager
    private CommunicationManager com_manager;
    // The Items Manager
    private ItemManager item_manager;
    // The Graphical Items Manager
    private GraphicsManager graphics_manager;
    // The World
    private World flunker_world;
    // The Refresh Task
    private RefreshTask refresh_task;
    // The Refresh Timer to control the fps rate
    private Timer refresh_timer;
    
    //Constructor
    public FlunkerManager(FlunkerMIDlet midlet){
        
        flunker_midlet = midlet; // The MIDlet
        flunker_canvas = new FlunkerCanvas(this); // The Canvas
        flunker_display = Display.getDisplay(flunker_midlet); // The Display
        com_manager = new CommunicationManager(); // The Comunication Manager
        refresh_timer = new Timer();// The Refresh Timer to control the fps rate
    }
    
    public void startApp(){
        
        try{
            
            flunker_world = loadWorld("/Resources/pogoroo.m3g");
            graphics_manager = new GraphicsManager(this); // The Graphics Manager
            item_manager = new ItemManager(this); // The Item Manager
            
            loadPlayers(); //TEMPORARIO, ISTO VAI INICIALIZAR OS ITENS
            graphics_manager.setupAspectRatio();
            
        } catch(Exception e) {
            handleException(e);
        }
        
        //Set Flunker Canvas to be visible on the display
        flunker_display.setCurrent(flunker_canvas);
        
        refresh_task = new RefreshTask();
        
        // schedule a repeating timer to give us a framerate of 20fps.
        refresh_timer.schedule(refresh_task, 0, 50);
    }
    
    public void destroyApp(){
        
        flunker_midlet      = null;
        flunker_canvas      = null;
        flunker_display     = null;
        com_manager         = null;
        item_manager        = null;
        graphics_manager    = null;
        flunker_world       = null;
        refresh_task        = null;
        refresh_timer       = null;
        
    }
    
    private World loadWorld(String m3g_filename)throws IOException{     
        // load the world from the M3G file
        return (World)Loader.load(m3g_filename)[0];
    }
    
    private void loadPlayers() throws IOException{
        //this will put one player of type pogoroo
        item_manager.addItem(1,'p');
    }
    
    
    /**
     * This method is responsable for handling the user action, for now it will have direct effect on the user character, later it should handle network also
     */
    public void reportUserAction(int action_code){
    
        item_manager.setAction(1,action_code);
        
    }
    
    public void paint(Graphics g){
        graphics_manager.paint(g);
    }
    
    private void updateItens(int startTime){
        item_manager.updateItens(startTime);                
    }
    
    public FlunkerCanvas getCanvas(){
        return flunker_canvas;
    }
    
    public World getWorld(){
        return flunker_world;
    }
    
    public void handleException(Exception e){
        e.printStackTrace();
    }
    
    public void updateWorld() {
    }
    
    public void addPlayer() {
    }
    
    public void getPlayers() {
    }
    
    /**
     * Inner class for refreshing the view.
     */
    private class RefreshTask extends TimerTask {
        public void run() {
            // Get the canvas to repaint itself.
            if(flunker_canvas != null && graphics_manager.getGraphics3D() != null && flunker_world != null) {
                int startTime = (int)System.currentTimeMillis();
                
                // update the control and game AI
                updateItens(startTime);
                // Update the world to the current time.
                int validity = flunker_world.animate(startTime);
                // Cause a render
                graphics_manager.render();
            }
        }
    }
    
}